package com.roast.pages;

import com.roast.domain.UserData;
import com.roast.services.my.MyServicesFacade;
import com.roast.services.messges.GetSystemMessageFacade;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionAttribute;
import org.apache.tapestry5.ioc.annotations.InjectService;

public class Login {

    @InjectService("MyServicesFacade")
    MyServicesFacade myServices;

    @InjectService("GetSystemMessageFacade")
    GetSystemMessageFacade getSystemMessage;

    @SessionAttribute
    boolean loginSA;

    @SessionAttribute
    UserData userSA;

    @Property
    String email;

    @Property
    String password;

    @Property
    String systemMessage;

    @OnEvent(value = "submit", component = "login")
    Object onFormSubmit() {
        UserData user = new UserData();
        user.setEmail(email);
        user.setPassword(password);

        int signal = myServices.login(user);
        if (signal == 10200) {
            userSA = user;
            loginSA = true;
            return Main.class;
        } else {
            systemMessage = getSystemMessage.login(signal);
        }
        return null;
    }

    void onActivate(String systemMessage) {
        this.systemMessage = systemMessage;
    }

    String onPassivate() {
        return systemMessage;
    }
}
